package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import Database.DelDbFunctions;
import Database.GetDbFunctions;
import Database.Recs.AccountRec;
import Database.Recs.CategoryRec;

public class AddDims extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4873059520548317111L;
	private AddItem aItem = new AddItem(this);
	
	private JList listSelectionMenu;
	private JScrollPane scrollPanelForSelectionMenu;
	private JScrollPane scrollPanelForCategory;
	private JScrollPane scrollPanelFroAccounts;
	private JTable tableFroCategory;
	private JTable tableForAccounts;
	private DefaultTableModel tableModelCategory;
	private DefaultTableModel tableModelAccount;
	private JButton buttonDelete;
	private JButton buttonNewItem;
	private JButton buttonClose;
	private String selectedValue;
	private MainWindow main;

	
	public AddDims(MainWindow mw) 
	{
		this.main = mw;
		initComponents();
		initData();
	}
	
	private void initComponents() {
		this.setTitle("Dimensions overview");
		this.setIconImage(new ImageIcon("coins-icon.png").getImage());
		setLayout(null);
		add(getButtonDelete());
		add(getButtonNewItem());
		add(getButtonClose());
		add(getScrollPanelFroAccounts());
		add(getScrollPanelForCategory());
		add(getScrollPanelForSelectionMenu());
		setSize(694, 264);
		

		selectedValue = "Category";
		aItem.setDim(0);
		scrollPanelForCategory.setVisible(true);
		scrollPanelFroAccounts.setVisible(false);
	}
	
	public void initData(){
		
		int rowCounts = tableModelCategory.getRowCount();
		for (int i = rowCounts-1; i >=0 ; i--) {
			tableModelCategory.removeRow(i);
		}
		rowCounts = tableModelAccount.getRowCount();
		for (int i = rowCounts-1; i >=0 ; i--) {
			tableModelAccount.removeRow(i);
		}
		
		ArrayList<CategoryRec> categories = GetDbFunctions.getCategory();
		if(categories!=null)
		{	
			for (int i = 0; i < categories.size(); i++) {
				tableModelCategory.addRow(new Object[] {categories.get(i).getId(), categories.get(i).getName(), categories.get(i).getDescription()});
			}
			tableFroCategory.repaint();
		}
		
		ArrayList<AccountRec> accounts = GetDbFunctions.getAccounts();
		if(accounts!=null)
		{	
			for (int i = 0; i < accounts.size(); i++) {
				tableModelAccount.addRow(new Object[] {accounts.get(i).getId(), accounts.get(i).getAccountNumber(), accounts.get(i).getBankNumber(), accounts.get(i).getDescription()});
			}
			tableForAccounts.repaint();
		}
		
	}

	private JButton getButtonClose() {
		if (buttonClose == null) {
			buttonClose = new JButton();
			buttonClose.setText("Close");
			buttonClose.setBounds(540, 190, 137, 25);
			buttonClose.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					onButtonClose(event);
				}
			});
		}
		return buttonClose;
	}

	private JButton getButtonNewItem() {
		if (buttonNewItem == null) {
			buttonNewItem = new JButton();
			buttonNewItem.setText("Add new item");
			buttonNewItem.setBounds(536, 37, 139, 26);
			buttonNewItem.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					onButtonNewItem(event);
				}
			});
		}
		return buttonNewItem;
	}

	private JButton getButtonDelete() {
		if (buttonDelete == null) {
			buttonDelete = new JButton();
			buttonDelete.setText("Delete selected");
			buttonDelete.setBounds(537, 83, 139, 26);
			buttonDelete.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					onButtonDelete(event);
				}
			});
		}
		return buttonDelete;
	}

	

	private JScrollPane getScrollPanelForCategory() {
		if (scrollPanelForCategory == null) {
			scrollPanelForCategory = new JScrollPane();
			scrollPanelForCategory.setBounds(121, 19, 400, 200);
			scrollPanelForCategory.setViewportView(getTableFroCategory());
		}
		return scrollPanelForCategory;
	}

	private JScrollPane getScrollPanelFroAccounts() {
		if (scrollPanelFroAccounts == null) {
			scrollPanelFroAccounts = new JScrollPane();
			scrollPanelFroAccounts.setBounds(121, 19, 400, 200);
			scrollPanelFroAccounts.setViewportView(getTableForAccounts());
		}
		return scrollPanelFroAccounts;
	}

	private JTable getTableFroCategory() {
		if (tableFroCategory == null) {
			tableFroCategory = new JTable();
			
			tableModelCategory = new DefaultTableModel(	new Object[][] {   },new String[] { "ID", "Category Name", "Description", }) 
								{
									private static final long serialVersionUID = 1L;
									Class<?>[] types = new Class<?>[] { Integer.class, String.class, String.class, };
	
									public Class<?> getColumnClass(int columnIndex) {
										return types[columnIndex];
									}
								};
			
			tableFroCategory.setModel(tableModelCategory);
		}
		
		return tableFroCategory;
	}
	
	private JTable getTableForAccounts() {
		if (tableForAccounts == null) {
			tableForAccounts = new JTable();
			
			tableModelAccount = new DefaultTableModel(	new Object[][] {   },new String[] { "ID", "Account Number", "Bank number", "Description" }) 
								{
									private static final long serialVersionUID = 1L;
									Class<?>[] types = new Class<?>[] { Integer.class, String.class, Integer.class, String.class, };
	
									public Class<?> getColumnClass(int columnIndex) {
										return types[columnIndex];
									}
								};
			
			tableForAccounts.setModel(tableModelAccount);
		}
		
		return tableForAccounts;
	}

	private JScrollPane getScrollPanelForSelectionMenu() {
		if (scrollPanelForSelectionMenu == null) {
			scrollPanelForSelectionMenu = new JScrollPane();
			scrollPanelForSelectionMenu.setBounds(17, 19, 82, 199);
			scrollPanelForSelectionMenu.setViewportView(getListSelectionMenu());
		}
		return scrollPanelForSelectionMenu;
	}

	private JList getListSelectionMenu() {
		if (listSelectionMenu == null) {
			listSelectionMenu = new JList();
			DefaultListModel listModel = new DefaultListModel();
			listModel.addElement("Category");
			listModel.addElement("Accounts");
			listSelectionMenu.setModel(listModel);
			listSelectionMenu.addListSelectionListener(new ListSelectionListener() {
	
				public void valueChanged(ListSelectionEvent event) {
					onMenuListChange(event);
				}
			});
		}
		return listSelectionMenu;
	}

	
	//ActionListeners
	private void onMenuListChange(ListSelectionEvent event) {
		Object[] selectedValues = (Object[]) ((JList)event.getSource()).getSelectedValues();
//		String selectedValue;
		
		if(selectedValues.length == 1)
		{
			selectedValue = (String) selectedValues[0];
			if(selectedValue.equalsIgnoreCase("Category"))
			{
				scrollPanelForCategory.setVisible(true);
				scrollPanelFroAccounts.setVisible(false);
				aItem.setDim(0);
			}
			else if(selectedValue.equalsIgnoreCase("Accounts"))
			{
				scrollPanelForCategory.setVisible(false);
				scrollPanelFroAccounts.setVisible(true);
				aItem.setDim(2);
			}
		}
	}

	private void onButtonNewItem(ActionEvent event) {
		aItem.setVisible(true);
	}

	private void onButtonDelete(ActionEvent event) {
		int[] selected;
		
		if(selectedValue.equalsIgnoreCase("Category"))
		{
			selected = tableFroCategory.getSelectedRows();
			for(int i=selected.length-1;i>=0;i--)
			{
				DelDbFunctions.delCategory((Integer)tableModelCategory.getValueAt(selected[i], 0));
				tableModelCategory.removeRow(selected[i]);
			}
		}
		else if(selectedValue.equalsIgnoreCase("Accounts"))
		{
			selected = tableForAccounts.getSelectedRows();
			for(int i=selected.length-1;i>=0;i--)
			{
				DelDbFunctions.delAccount((Integer)tableModelAccount.getValueAt(selected[i], 0));
				tableModelAccount.removeRow(selected[i]);
			}
		}
	}

	private void onButtonClose(ActionEvent event) {
		main.initData();
		this.setVisible(false);
	}

}
